package com.peng.sell.controller;

import com.peng.sell.dto.OrderDTO;
import com.peng.sell.enums.ResponseCodeEnum;
import com.peng.sell.exception.SellException;
import com.peng.sell.service.OrderService;
import com.peng.sell.utils.PageUtil;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/list")
    public String list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize,
                       Map<String, Object> dataMap) {
        PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize);
        Page<OrderDTO> data = orderService.findList(pageRequest);
        dataMap.put("data", data);
        dataMap.put("currentPage",pageNum);
        dataMap.put("pageSize",pageNum);
        dataMap.put("pagination",PageUtil.getPagination(pageNum,data.getTotalPages()));

        return "/order/list";
    }

    @GetMapping("/cancel")
    public String cancel(String orderId,Map<String,Object> map){
        try {
            OrderDTO orderDTO = orderService.findOne(orderId);
            orderService.cancel(orderDTO);
        }catch (SellException e){
            log.error("【订单取消】出现异常：e：{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return "/common/error";
        }
        map.put("msg",ResponseCodeEnum.ORDER_CANCEL_SUCCESS.getDesc());
        map.put("url","/sell/seller/order/list");
        return "/common/success";


    }

    @GetMapping("/detail")
    public String detail(String orderId,Map<String,Object> map){
        OrderDTO orderDTO;
        try {
            orderDTO = orderService.findOne(orderId);
        }catch (SellException e){
            log.error("【卖家端查询订单详情】出现异常：e：{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return "/common/error";
        }
        map.put("data",orderDTO);
        return "/order/detail";
    }



    @GetMapping("/finish")
    public String finish(String orderId,Map<String,Object> map){

        try {
            OrderDTO orderDTO = orderService.findOne(orderId);
            orderService.finish(orderDTO);
        }catch (SellException e){
            log.error("【订单完结】出现异常：e：{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return "/common/error";
        }
        map.put("msg",ResponseCodeEnum.ORDER_FINISH_SUCCESS.getDesc());
        map.put("url","/sell/seller/order/list");
        return "/common/success";


    }
}
